import { Controller, Get, Post, Body, Delete, Param } from '@nestjs/common';
import { QuestionsService } from './questions.service';
import { ListItemDto, QuestionItemDto } from './dto/question.dto';
import { Topic } from './entities/topic.entity';

@Controller('questions')
export class QuestionsController {
  constructor(private readonly questionsService: QuestionsService) { }

  @Get('list')
  async getQuestions(): Promise<ListItemDto[]> {
    const topics: Topic[] = (await this.questionsService.getQuestions()) || [];

    console.log(topics);

    const list: ListItemDto[] = topics.map((topic) => {
      return {
        ...topic,
        tags: JSON.parse(topic.tags) as string[],
        questions: JSON.parse(topic.questions) as QuestionItemDto[],
      };
    });
    return list;
  }

  // 修改
  @Post('update')
  async updateQuestion(@Body() body: any) {
    return await this.questionsService.updateQuestion(body);
  }

  // 新增
  @Post('add')
  async addQuestion(@Body() body: any) {
    return await this.questionsService.addQuestion(body);
  }

  // 删除
  @Delete('delete/:id')
  async deleteQuestion(@Param('id') id: number) {
    return await this.questionsService.deleteQuestion(id);
  }
}